package com.luyuan.crm.question.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.Member;
import com.luyuan.crm.member.service.MemberService;
import com.luyuan.crm.question.model.Question;
import com.luyuan.crm.question.model.QuestionExtend;
import com.luyuan.crm.question.model.QuestionValue;
import com.luyuan.crm.question.service.QuestionService;
import com.luyuan.crm.question.service.QuestionValueService;
import com.ruijie.util.StringUtils;
import com.ruijie.web.jsf.action.BaseAction;

@Component("questionValueAction")
@Scope("view")
public class QuestionValueAction extends BaseAction implements InitializingBean {

    @Resource(name = "memberService")
    private MemberService memberService;

    @Resource(name = "questionService")
    private QuestionService questionService;

    @Resource(name = "questionValueService")
    private QuestionValueService questionValueService;

    private Member member;

    private Question question;

    private List<QuestionValue> questionValues;

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public List<QuestionValue> getQuestionValues() {
        if (questionValues != null) {
            return questionValues;
        }

        questionValues = questionValueService.findByCustomer(member, question);
        if (questionValues == null || questionValues.isEmpty()) {
            questionValues = new ArrayList<QuestionValue>();
            for (QuestionExtend extend : question.getQuestionExtends()) {
                QuestionValue questionValue = new QuestionValue();
                questionValue.setExtendValue(extend.getDefaultValue());
                questionValue.setQuestion(question);
                questionValue.setQuestionExtend(extend);
                questionValue.setCustomer(member);
                questionValues.add(questionValue);
            }
        }
        return questionValues;
    }

    public void clearQuestionValues() {
        questionValues = null;
    }

    public void updateQuestionValues() {
        for (QuestionValue questionValue : questionValues) {
            questionValueService.updateQuestionValue(questionValue);
        }
        addGlobalI18nMessage("crud_update_success_msg");
    }

    public Member getMember() {
        if (this.member == null) {
            member = new Member();
        }
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String memberId = getRequestParameter("memberId");
        if (StringUtils.isNotEmpty(memberId)) {
            member = memberService.getById(memberId);
        }

        String questionId = getRequestParameter("questionId");
        if (StringUtils.isNullOrEmpty(questionId)) {
            questionId = "1";
        }
        question = questionService.get(questionId);
    }
}
